package ee.graphicscard.controller.graphicscardcontroller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import ee.graphicscard.controller.Command;
import ee.graphicscard.controller.forms.GraphicsCardForm;
import ee.graphicscard.model.dao.GraphicsCardDAO;
import ee.graphicscard.model.dao.GraphicsCardDAOImpl;
import ee.graphicscard.model.dto.GraphicsCard;

public class GetGraphicsCardById implements Command
{
	private Logger logger = Logger.getLogger(GetGraphicsCardById.class);
	int operationResult = 0;
	
	public int executeCommand(HttpServletRequest request,
			HttpServletResponse response) 
	{
		try
		{
			int graphicsCard_id = 0;
			GraphicsCard graphicsCardFound = null;
			GraphicsCardDAO graphicsCardImpl = new GraphicsCardDAOImpl();
			GraphicsCardForm formForGraphicsCardData = new GraphicsCardForm();
			graphicsCard_id = Integer.parseInt(request.getParameter("id"));
			graphicsCardFound = graphicsCardImpl.findById(graphicsCard_id);
			if (graphicsCardFound != null)
			{
				//Lets give GraphicsCard found to a form, so its data could be converted into 
				//String type variables and then stored in a GraphicsCardForm object
				formForGraphicsCardData.convertDataFromModelToStringFormation(graphicsCardFound);
				//Data stored in a form object is in type String, lets put it on a request object
				request.setAttribute("formForGraphicsCardData", formForGraphicsCardData);
				
				operationResult = 1;
			}
			else
			{
				request.setAttribute("errorsForUsers", "Graafikakaarti identifikaatoriga " + graphicsCard_id + " ei leitud!");
				operationResult = -1;
			}
		}
		catch (Exception e)
		{
			logger.warn(e.getMessage());
			operationResult = 0;
			return operationResult;
		}
		
		return operationResult;
	}
}
